package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.ulits.DateUtils;
import com.itheima.ulits.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 会员服务实现类
 * @author ：zengjie
 * @date ：2020/11/14
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    /**
     * 根据电话号码查询会员信息
     * @param telephone
     * @return
     */
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    /**
     * 注册会员
     * @param member
     */
    @Override
    public void add(Member member) {
        member.setRegTime(new Date());//注册时间
        //对页面传入的密码进行加密,如果密码为空就不加密,实现功能复用
        if(!StringUtils.isEmpty(member.getPassword())){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }

    /**
     * 近一年会员数量统计
     * @return
     */
    @Override
    public Map getMemberReport() {
        //创建返回map
        Map map = new HashMap();
        //日历对象,获取前一年的日期
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);
        //存放日期,格式为 年-月
        List<String> months = new ArrayList<String>();
        for(int i=0;i<12;i++){
            calendar.add(Calendar.MONTH,1);
            months.add(new SimpleDateFormat("yyyy-MM").format(calendar.getTime()));
        }

        List<Integer> memberCount = new ArrayList<>();
        //查询每一个月的累计会员数量
        for (String month : months) {
            String endDate = month+"-31";
            //查询
            Integer mc = memberDao.findMemberCountBeforeDate(endDate);
            memberCount.add(mc);
        }
        map.put("months",months);
        map.put("memberCount",memberCount);
        return map;
    }

    /**
     * 会员性别，年龄占比饼图
     */
    @Override
    public List<Map> getMemberComposedReport() {
        List list = new ArrayList<>();

        //会员性别占比饼图
        Map sexMap = getSexCount();
        System.out.println(sexMap);
        list.add(sexMap);

        //会员年龄占比饼图 110101198003071399
        Map ageMap = getAgeCount();
        list.add(ageMap);


        return list;
    }

    //会员年龄占比查询
    private Map getAgeCount() {
        Map ageMap = new HashMap<>();
        List<String> ages = new ArrayList<>();
        List<Member> memberList = memberDao.findAll();
        List<Map> agesCounts = new ArrayList<>();
        Map map = new LinkedHashMap();
        map.put("0-18岁", "0");
        map.put("18-30岁", "0");
        map.put("30-45岁", "0");
        map.put("45以上", "0");
        map.put("其他", "0");
        for (Member member : memberList) {
            String idCard = member.getIdCard();
            if (idCard != null && idCard.length() == 18 ) {
                String birthdayDate = idCard.substring(6, 10);
                String todayDate = new SimpleDateFormat("YYYY").format(new Date());
                int age = Integer.parseInt(todayDate) - Integer.parseInt(birthdayDate);
                System.out.println(age);
                if (age > 45) {
                    map.put("45以上",Integer.parseInt(map.get("45以上")+"" + 1));
                } else if (age > 30 && age <= 45) {
                    map.put("30-45岁",Integer.parseInt(map.get("30-45岁")+"") + 1);
                }else if (age > 18 && age <= 30) {
                    map.put("18-30岁",Integer.parseInt(map.get("18-30岁")+"") + 1);
                } else if (age >= 0 && age <= 18) {
                    map.put("0-18岁", Integer.parseInt(map.get("0-18岁") + "") + 1);
                }
            }else {
                map.put("其他",Integer.parseInt(map.get("其他") + "") + 1);
            }

        }
        for (Object keys : map.keySet()) {
            HashMap<String, String> hashMap = new HashMap<>();
            String key = (String) keys;
            ages.add(key);
            hashMap.put("name", key);
            hashMap.put("value", map.get(key)+"");
            agesCounts.add(hashMap);
        }
        System.out.println(ageMap);
        ageMap.put("ages",ages);
        ageMap.put("agesCounts",agesCounts);
        return ageMap;
    }

    //会员性别占比查询
    private Map getSexCount() {
        Map sexMap = new HashMap<>();
        List<String> sexs = new ArrayList<>();
        List<Map> sexsCounts = new ArrayList<>();
        List<Map> sexsCountsTemp = memberDao.getMemberSexReport();
        for (Map sexsCount : sexsCountsTemp) {
            Map map = new HashMap<>();
            String sex = (String) sexsCount.get("sex");
            String sexCount = sexsCount.get("sexCount")+"";
            if (sex == null) {
                map.put("name", "其他");
                sexs.add("其他");
            } else {
                if (("2").equals(sex)) {
                    map.put("name", "女");
                    sexs.add("女");
                } else if (("1").equals(sex)) {
                    map.put("name", "男");
                    sexs.add("男");
                }
            }
            map.put("value", sexCount);
            System.out.println(map);
            sexsCounts.add(map);
        }
        sexMap.put("sexs",sexs);
        sexMap.put("sexsCounts",sexsCounts);
        return sexMap;
    public Map getMemberByDate(String startDate,String endDate) {
        //定义返回结果map
        try {
            Map map = new HashMap();
            List<String> monthBetween = DateUtils.getMonthBetween(startDate, endDate,"yyyy-MM");
            List<Integer> memberCount = new ArrayList<>();
            for (String s : monthBetween) {
                String startDate1 = s + "-01";
                String endDate1 = s + "-31";
                Integer mc = memberDao.findMemberCountBy2Date(startDate1,endDate1);
                memberCount.add(mc);
            }
            map.put("months",monthBetween);
            map.put("memberCount",memberCount);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
